package com.bootx.job;

import com.bootx.entity.Product;
import com.bootx.entity.ProductCategory;
import com.bootx.service.ArticleService;
import com.bootx.service.ProductCategoryService;
import com.bootx.service.ProductService;
import com.bootx.util.AiBotUtils;
import jakarta.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author black
 */
//@Component
public class AiBotJob {

    @Resource
    private ProductCategoryService productCategoryService;

    @Resource
    private ProductService productService;
    @Resource
    private JdbcTemplate jdbcTemplate;
    @Resource
    private ArticleService articleService;

    @Scheduled(fixedRate = 1000*60*60*24)
    public void getCategory(){
        /*Map<String, ProductCategory> category = AiBotUtils.getCategory();
        Set<String> strings = category.keySet();
        strings.forEach(otherId -> {
            ProductCategory productCategory = category.get(otherId);
            ProductCategory save = productCategoryService.save(productCategory);
            if(!productCategory.getChildren().isEmpty()){
                productCategory.getChildren().forEach(child -> {
                    child.setParent(save);
                    productCategoryService.save(child);
                });
            }
        });*/
        // 分类下面得数据
        /*List<ProductCategory> all = productCategoryService.findAll();
        for (ProductCategory productCategory : all) {
            List<Product> item = AiBotUtils.getItem(productCategory.getOtherId());
            item.forEach(product -> {
               Product product1 = productService.create(product);
                jdbcTemplate.update("insert into product_productcategories(products_id, productCategories_id) VALUES (?,?) ;",product1.getId(),productCategory.getId());

            });
        }*/

        /*Map<String, List<Article>> news = AiBotUtils.getNews();
        news.keySet().forEach(key -> {
            news.get(key).forEach(article -> {
                jdbcTemplate.update("insert into article(createdDate, lastModifiedDate, version, date, memo, source, title, url) VALUES (NOW(),NOW(),0,?,?,?,?,?)",article.getDate(),article.getMemo(),article.getSource(),article.getTitle(),article.getUrl());
            });
        });*/
        /*List<Article> news1 = AiBotUtils.getNews1();
        news1.forEach(item->{
            jdbcTemplate.update("insert into article(createdDate, lastModifiedDate, version, memo, title, url, cover, articleCategory_id) values (NOW(),NOW(),0,?,?,?,?,?)",item.getMemo(),item.getTitle(),item.getUrl(),item.getCover(),9);
        });*/

        /*List<Article> all = articleService.findAll();
        for (Article article : all) {
            Article articleDetail = AiBotUtils.getArticleDetail(article.getUrl());
            article.setPublishDate(articleDetail.getPublishDate());
            article.setMemo(articleDetail.getMemo());
            article.setContent(articleDetail.getContent());
            articleService.update(article);
        }*/
        List<Product> all1 = productService.findAll();
        for (Product product : all1) {
            Product articleDetail = AiBotUtils.getProductDetail(product.getOtherUrl());
            if(articleDetail==null){
                continue;
            }
            product.setPublishDate(articleDetail.getPublishDate());
            product.setMemo(articleDetail.getMemo());
            product.setContent(articleDetail.getContent());
            product.setIcon(articleDetail.getIcon());
            product.setLogo(articleDetail.getLogo());
            product.setSeoTitle(articleDetail.getSeoTitle());
            product.setSeoDescription(articleDetail.getSeoDescription());
            product.setDescription(articleDetail.getDescription());
            product.setImage(articleDetail.getImage());
            productService.update(product);
        }

    }
}